package wang.hu.bos.service.Impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wang.hu.bos.dao.WayBillDao;
import wang.hu.bos.dao.index.WayBillIndexDao;
import wang.hu.bos.domain.take_delivery.WayBill;
import wang.hu.bos.service.WayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {
	@Autowired
	private WayBillDao wayBillDao;
	@Autowired
	private WayBillIndexDao wayBillIndexDao;

	@Override
	public void save(WayBill wayBill) {
		WayBill persistWayBill = wayBillDao.findByWayBillNum(wayBill
				.getWayBillNum());
		if (wayBill.getOrder() == null || wayBill.getOrder().getId() == null) {
			wayBill.setOrder(null);
		}
		if (persistWayBill == null || persistWayBill.getId() == null) {
			wayBill.setSignStatus(1);
			wayBillDao.save(wayBill);
			wayBillIndexDao.save(wayBill);
		} else {
			Integer id = persistWayBill.getId();
			try {
				BeanUtils.copyProperties(persistWayBill, wayBill);
				persistWayBill.setId(id);
				persistWayBill.setSignStatus(1);
				wayBillIndexDao.save(persistWayBill);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	@Override
	public Page<WayBill> pageData(Pageable pageable, WayBill wayBill) {
		if (StringUtils.isBlank(wayBill.getWayBillNum())
				&& StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress())
				&& StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
			return wayBillDao.findAll(pageable);
		}
		BoolQueryBuilder query = new BoolQueryBuilder();
		if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
			// 运单号查询
			QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum",
					wayBill.getWayBillNum());
			query.must(tempQuery);
		}
		if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
			// 发货地 模糊查询
			// 情况一： 输入"北" 是查询词条一部分， 使用模糊匹配词条查询
			QueryBuilder wildcardQuery = new WildcardQueryBuilder(
					"sendAddress", "*" + wayBill.getSendAddress() + "*");

			// 情况二： 输入"北京市海淀区" 是多个词条组合，进行分词后 每个词条匹配查询
			QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(
					wayBill.getSendAddress()).field("sendAddress")
					.defaultOperator(Operator.AND);

			// 两种情况取or关系
			BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
			boolQueryBuilder.should(wildcardQuery);
			boolQueryBuilder.should(queryStringQueryBuilder);

			query.must(boolQueryBuilder);
		}
		if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
			// 收货地 模糊查询
			QueryBuilder wildcardQuery = new WildcardQueryBuilder("recAddress",
					"*" + wayBill.getRecAddress() + "*");
			query.must(wildcardQuery);
		}
		if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
			// 速运类型 等值查询
			QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
					wayBill.getSendProNum());
			query.must(termQuery);
		}
		if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
			// 速运类型 等值查询
			QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
					wayBill.getSendProNum());
			query.must(termQuery);
		}
		if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
			// 签收状态查询
			QueryBuilder termQuery = new TermQueryBuilder("signStatus",
					wayBill.getSignStatus());
			query.must(termQuery);
		}
		SearchQuery searchQuery = new NativeSearchQuery(query);
		searchQuery.setPageable(pageable); // 分页效果
		// 有条件查询 、查询索引库
		return wayBillIndexDao.search(searchQuery);

	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		return wayBillDao.findByWayBillNum(wayBillNum);
	}

	@Override
	public void sycnWayBill() {
		List<WayBill> list = wayBillDao.findAll();
		for (WayBill wayBill : list) {
			wayBillIndexDao.save(wayBill);
		}

	}

	@Override
	public List<WayBill> findWayBills(WayBill wayBill) {
		// 判断WayBill 中条件是否存在
		if (StringUtils.isBlank(wayBill.getWayBillNum())
				&& StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress())
				&& StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
			// 无条件查询 、查询数据库
			return wayBillDao.findAll();
		} else {
			// 查询条件
			// must 条件必须成立 and
			// must not 条件必须不成立 not
			// should 条件可以成立 or
			BoolQueryBuilder query = new BoolQueryBuilder(); // 布尔查询 ，多条件组合查询
			// 向组合查询对象添加条件
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				// 运单号查询
				QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum",
						wayBill.getWayBillNum());
				query.must(tempQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				// 发货地 模糊查询
				// 情况一： 输入"北" 是查询词条一部分， 使用模糊匹配词条查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder(
						"sendAddress", "*" + wayBill.getSendAddress() + "*");

				// 情况二： 输入"北京市海淀区" 是多个词条组合，进行分词后 每个词条匹配查询
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(
						wayBill.getSendAddress()).field("sendAddress")
						.defaultOperator(Operator.AND);

				// 两种情况取or关系
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryStringQueryBuilder);

				query.must(boolQueryBuilder);
			}
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				// 收货地 模糊查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder(
						"recAddress", "*" + wayBill.getRecAddress() + "*");
				query.must(wildcardQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型 等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
						wayBill.getSendProNum());
				query.must(termQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型 等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
						wayBill.getSendProNum());
				query.must(termQuery);
			}
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				// 签收状态查询
				QueryBuilder termQuery = new TermQueryBuilder("signStatus",
						wayBill.getSignStatus());
				query.must(termQuery);
			}

			SearchQuery searchQuery = new NativeSearchQuery(query);
			// ElasticSearch 允许搜索分页查询，最大数据条数10000
			Pageable pageable = new PageRequest(0, 10000);
			searchQuery.setPageable(pageable); // 分页效果

			// 有条件查询 、查询索引库
			return wayBillIndexDao.search(searchQuery).getContent();
		}
	}

}
